package club.koorilkaze.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @program: JdbcUtils
 * @description: 获取文件 db.properties 中的数据 返回 Connection
 * @author: Hermit_Yoshino
 * @create: 2019-07-24 19:02
 **/
public class JdbcUtils {
    private static final String dbproprties = "db.properties";
    private static Properties properties = new Properties();

    /** 
     * @Description: 静态初始化代码获取 db.properties 中的连接数据库的内容
     * @param:  none
     * @author: Hermit_Yoshino
     * @date: 2019/7/24 18:49
     * @throws: IOException, ClassNotException
     * @return:  none
     * @version: 0.0.1
     */ 
    static {
        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(dbproprties);
        try {
            properties.load(inputStream);
            Class.forName(properties.getProperty("driverClassName"));
        } catch ( IOException e1 ) {
//            throw e1;
            System.err.println(e1.toString());
//            throw new RuntimeException( e1 );
        } catch ( ClassNotFoundException e2 ) {
//            throw new RuntimeException( e2 );
            System.err.println(e2.toString());
        }
    }

    /** 
     * @Description: 获取连接SQL的 Connection 对象
     * @param: []
     * @author: Hermit_Yoshino
     * @date: 2019/7/24 18:49
     * @throws: SQLException
     * @return: java.sql.Connection 
     * @version: 0.0.1
     */ 
    public static Connection dbConnection () throws SQLException{
        try {
            return DriverManager.getConnection(properties.getProperty("url")
                    , properties.getProperty("user")
                    , properties.getProperty("password"));
        } catch (SQLException e) {
//            e.printStackTrace();
//            throw new RuntimeException( e );
            throw e;
        }
    }
}